/**
 * Copyright 2019 [shujiang.yang], Inc. All rights reserved.
 * Email: 314938230@qq.com
**/
package awd.bj.jls.webs.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;

import awd.bj.jls.model.JshjModel;
import awd.bj.jls.model.ShgxModel;
import awd.bj.jls.webs.api.JlsServersApi;
import awd.bj.jls.webs.api.model.JshjAndShgxModelDO;
import awd.bj.jls.webs.api.model.User;
import awd.bj.jls.webs.utils.PagerResult;
import awd.bj.jls.webs.utils.QueryParam;
import awd.bj.jls.webs.utils.ResponseMessage;
import awd.bj.jls.webs.utils.Result;
import awd.bj.jls.webs.utils.ResultUtils;

@Controller
@RequestMapping("/jshj")
@RefreshScope
public class JshjAction extends BaseAction {

    @Autowired
    private JlsServersApi jlsServersApi;

    @RequestMapping({"/jshj.html" //首页
            ,"/jshj_enter.html" //单表录入页面
            ,"/jshj_jbxx_enter.html" //人员信息查询页面
            ,"/jshj_query.html" //信息查询
            ,"/jshj_jbxx_query.html" //关联基本信息查询
            ,"/6.html" //
            ,"/7.html"})
    public String index(Model model, HttpServletRequest request) {
        model.addAllAttributes(getBaseModelData());
        return getRenderFilePath(request);
    }

/**
 @GetMapping("/jshj")
 public ResponseMessage<PagerResult<JshjModel>> jshjQueryForPage(@RequestBody QueryParam param);
 @PostMapping("/jshj")
 public ResponseMessage<String> jshjSave(@RequestBody JshjModel jshjModel);
 @PutMapping("/jshj/{id}")
 public String jshjUpdate(@RequestParam(value="id") String id ,@RequestBody JshjModel jshjModel);
 */

    /**
     * 数据列表,关联基本信息
     * @param request
     * @param params 所有请求的参数集合
     * @return
     */
    @RequestMapping("/list2jbxx")
    @ResponseBody
    public Map<String, Object> list2jbxx(HttpServletRequest request,Map<String,Object> params) {
        QueryParam param = getBaseQueryParam(request,params);
        ResponseMessage<PagerResult<JshjModel>> list = jlsServersApi.jshjQueryForPage(param);
        Map<String, Object> result = Maps.newHashMap();
        result.put("total", list.getResult().getTotal());
        result.put("rows", list.getResult().getData());
        return result;
    }

    /**
     * 数据列表
     * @param request
     * @param params 所有请求的参数集合
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request,Map<String,Object> params) {
        QueryParam param = getBaseQueryParam(request,params);
        System.err.println("=====================杨广=======================");
        System.err.println("=====================jshj/list===================");
        System.err.println(JSON.toJSON(param));
        System.err.println("==================================================");
        ResponseMessage<PagerResult<JshjModel>> list = jlsServersApi.jshjQueryForPage(param);
        Map<String, Object> result = Maps.newHashMap();
        result.put("total", list.getResult().getTotal());
        result.put("rows", list.getResult().getData());
        return result;
    }
    /**
     * 数据保存操作
     * @param model
     * @return
     */
    @RequestMapping("/qshjsave")
    @ResponseBody
    public Result<?> save(@ModelAttribute JshjModel model){
        User users = getCurrentUser();
        try {
            model.setState("R2");
            model.setJsbh(users.getJsbh());
            model.setCreatetime(new Date());
            model.setCreator(users.getUsername());
            System.err.println("==========================杨广=========================");
            System.err.println("==========================save===========================");
            System.err.println(JSON.toJSON(model));
            System.err.println("==========================================================");
            return ResultUtils.success(jlsServersApi.jshjSave(model));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }

    /**
     * 数据保存操作
     * @param model
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result<?> save(@ModelAttribute JshjModel model, @ModelAttribute ShgxModel model1,
                            @ModelAttribute JshjAndShgxModelDO jshjAndShgxModel){
       User users = getCurrentUser();
       List<String> list=new ArrayList<>();
       if (jshjAndShgxModel.getJszjlx1()!=null&&!"".equals(jshjAndShgxModel.getJszjlx1())){
       list.add(jshjAndShgxModel.getJszjlx1());}
        if (jshjAndShgxModel.getJszjlx2()!=null&&!"".equals(jshjAndShgxModel.getJszjlx2())){
        list.add(jshjAndShgxModel.getJszjlx2());}
        if (jshjAndShgxModel.getJszjlx3()!=null&&!"".equals(jshjAndShgxModel.getJszjlx3())){
        list.add(jshjAndShgxModel.getJszjlx3());}
        try {
            for (int i=0;i<jshjAndShgxModel.getJsxm().size();++i){
                if("".equals(jshjAndShgxModel.getJsxm().get(i))){continue;}
                model1.setDh(jshjAndShgxModel.getDh().get(i));
                model1.setDz(jshjAndShgxModel.getDz().get(i));
                model1.setGx(jshjAndShgxModel.getGx().get(i));
                model1.setGzdw(jshjAndShgxModel.getGzdw().get(i));
                model1.setXm(jshjAndShgxModel.getJsxm().get(i));
                model1.setXb(jshjAndShgxModel.getHjrxb().get(i));
                model1.setZjh(jshjAndShgxModel.getZjh().get(i));
                model1.setZjlx(list.get(i));

                model1.setState("R2");
                model1.setJsbh(users.getJsbh());
                model1.setCreatetime(new Date());
                model1.setCreator(users.getUsername());

                // 数据测试
                System.err.println("===============杨广===============");
                System.err.println("===========save数据测试===========");
                System.err.println("=====" + JSON.toJSONString(model1));
                System.err.println("====================================");

            }
            jlsServersApi.shgxSave(model1);
            return ResultUtils.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }

    /**
     * 数据更新
     * @param model
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result<?> update(@ModelAttribute JshjModel model) {
        User users = getCurrentUser();
        try {
            model.setUpdator(users.getUsername());
            model.setUpdatetime(new Date());
            return ResultUtils.success(jlsServersApi.jshjUpdate(model.getId(),model));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }

    /**
     * 删除操作
     * @param model
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result<?> delete(@ModelAttribute JshjModel model) {
        User users = getCurrentUser();
        try {
            model.setState("R3");
            model.setUpdator(users.getUsername());
            model.setUpdatetime(new Date());
            return ResultUtils.success(jlsServersApi.jshjUpdate(model.getId(),model));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "删除失败");
        }
    }

}